package com.example.controller;

import com.example.pojo.Item;
import com.example.pojo.ItemReq;
import com.example.pojo.ResultBean;
import com.example.service.EsItemService;
import com.example.service.ItemsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/items")
public class ItemsController {
    @Autowired
    private ItemsService itemsService;

    @GetMapping("list")
    public ResultBean list(ItemReq itemReq){
        PageHelper.startPage(itemReq.getPageNum(),itemReq.getPageSize());
        List<Item> items = itemsService.queryByCondition(itemReq);
        return ResultBean.success(new PageInfo(items));
    }

    @PostMapping("saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody Item item){
        System.out.println("新增或修改的数据：" + item);
        boolean rs = itemsService.saveOrUpdate(item);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    @DeleteMapping("deleteById/{id}")
    public ResultBean deleteById(@PathVariable("id") Long id){
        System.out.println("要删除的id：" + id);
        int rs = itemsService.deleteById(id);
        return rs > 0 ? ResultBean.success() : ResultBean.error();
    }
}
